import 'package:hxdj/common/base_page/base_page.dart';
import 'package:hxdj/common/dio/dio_http.dart';
import 'dart:convert';

class SelectServiceTimeViewModel extends BaseViewModel {
  List<ServiceTimeModel> dataList = [];

  int selectIndex = 0;

  @override
  requestData({Function(dynamic)? onSuccess, Function(dynamic)? onError, String? tId}) async {
    setState(ViewState.loading);

    // 去请求服务器数据.
    await Future.delayed(const Duration(microseconds: 500));
    Map<String, dynamic> resultMap =
        await DioHttpManager().request(MyApi.anmoV2GetAboutOrderTime, method: DioHttpManager.post, data: {"t_id": tId});
    dataList.clear();
    if (resultMap["code"] == 0) {
      // 请求成功.
      List<ServiceTimeModel> serviceTimeList = [];
      for (var item in resultMap["data"]) {
        serviceTimeList.add(ServiceTimeModel.fromMap(item));
      }
      dataList.addAll(serviceTimeList);
    }

    // 更新页面.
    setState(ViewState.normal);
  }

  //修改索引值
  selectIndexChange(int index) {
    selectIndex = index;
    notifyListeners();
  }
}

class ServiceTimeModel {
  final String? week;
  final String? days;
  final String? day;
  final String? dayTxt;
  final List<String>? orderTime;

  ServiceTimeModel({
    this.week,
    this.days,
    this.day,
    this.dayTxt,
    this.orderTime,
  });

  factory ServiceTimeModel.fromJson(String str) => ServiceTimeModel.fromMap(json.decode(str));

  String toJson() => json.encode(toMap());

  factory ServiceTimeModel.fromMap(Map<String, dynamic> json) => ServiceTimeModel(
        week: json["week"],
        days: json["days"],
        day: json["day"],
        dayTxt: json["dayTxt"],
        orderTime: json["order_time"] == null ? [] : List<String>.from(json["order_time"]!.map((x) => x)),
      );

  Map<String, dynamic> toMap() => {
        "week": week,
        "days": days,
        "day": day,
        "dayTxt": dayTxt,
        "order_time": orderTime == null ? [] : List<dynamic>.from(orderTime!.map((x) => x)),
      };
}
